package com.iplant.basic.controller.bfc;

import com.iplant.base.controller.BaseController;
import com.iplant.base.po.BasePO;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.annotation.trace.TraceAPI;
import com.iplant.base.utils.annotation.trace.TraceModuleAPI;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.service.po.bms.BMSEmployee;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/BFCRequestBasic")
@TraceModuleAPI("通用接口")
public class BFCRequestBasicController extends BaseController {


    public BFCRequestBasicController() {

    }

    /**
     * 查单条
     */
    @TraceAPI( value = "单条查询")
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request,@RequestParam(name="BasePO_Name") String wBasePOName) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);

            PoService<? extends BasePO> wPOService= GetPoService(wBasePOName);

            if(wPOService==null){
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<? extends BasePO> wServiceResult =wPOService.SelectPO(wLoginUser, wParams);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
             wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }




    /**
     * 条件查询
     */
    @TraceAPI("批量查询")
    @GetMapping("/All")
    public Object All(HttpServletRequest request,@RequestParam(name="BasePO_Name") String wBasePOName) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);

            Class<? extends BasePO> wClazz =GetBasePOClass( wBasePOName);

            if(wClazz==null){
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            PoService<? extends BasePO> wPOService= ORMUtils.GetPoService(wClazz);

            if(wPOService==null){
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            Pagination wPagination = GetPagination(request,wClazz);

            ServiceResult<? extends List<? extends BasePO>> wServiceResult =
                    wPOService.SelectPOList(wLoginUser, wParams, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 新增或更新
     */
    @TraceAPI(value = "新增或更新")
    @PostMapping("/Update")
    public Object Update(HttpServletRequest request,  @RequestBody Map<String, Object> wParam) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            PoService<? extends BasePO> wPOService= GetPoService(wParam);

            if(wPOService==null){
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<? extends BasePO> wServiceResult = wPOService.UpdateSelectPo(wLoginUser, wParam);


            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.getResult());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }




    /**
     * 新增或更新
     */
    @TraceAPI("批量新增或更新")
    @PostMapping("/UpdateList")
    public Object UpdateList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            PoService<? extends BasePO> wPOService= GetPoService(wParam);

            if(wPOService==null){
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<? extends List<? extends BasePO>> wServiceResult = wPOService.UpdatePoList(wLoginUser, wParam);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.getResult(), null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }



    /**
     * 批量激活或禁用
     */
    @TraceAPI("激活")
    @PostMapping("/Active")
    public Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);

            PoService<? extends BasePO> wPOService= GetPoService(wParam);

            if(wPOService==null){
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            ServiceResult<Integer> wServiceResult = wPOService.ActivePOList(wLoginUser, wParam);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 批量删除
     */
    @TraceAPI("批量删除")
    @PostMapping("/DeleteList")
    public Object DeleteList(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult;
        try {
            BMSEmployee wLoginUser = GetSession(request);
            PoService<? extends BasePO> wPOService= GetPoService(wParam);

            if(wPOService==null){
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            ServiceResult<Integer> wServiceResult = wPOService.DeletePOList(wLoginUser, wParam);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @TraceAPI("删除")
    @PostMapping("/Delete")
    public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            PoService<? extends BasePO> wPOService= GetPoService(wParam);

            if(wPOService==null){
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            ServiceResult<Integer> wServiceResult = wPOService.DeletePO(wLoginUser, wParam);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }



    @TraceAPI("激活变更")
    @PostMapping("/ActiveChange")
    public Object ActiveChange(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            PoService<? extends BasePO> wPOService= GetPoService(wParam);

            if(wPOService==null){
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }
            ServiceResult<Integer> wServiceResult = wPOService.ActivePOChange(wLoginUser, wParam);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            log.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


}
